package notice

import (
	"time"
	"vrcm/src/vrcm/app/admin/models/notice"
	dto "vrcm/src/vrcm/app/admin/service/dto/notice"
	"vrcm/src/vrcm/common/service"
)

type Notice struct {
	service.Service
}

func (e *Notice) Insert(c *dto.NoticeInsertReq) error {
	var err error
	var data notice.Notice
	now := time.Now().Unix()
	data.TenantId = c.TenantId
	data.NoticeInfo = c.NoticeInfo
	data.BindId = c.BindId
	data.CreateTime = now
	data.UpdateTime = now
	err = e.Orm.Create(&data).Error
	if err != nil {
		e.Log.Errorf("创建公告失败:%s \r\n", err)
		return err
	}
	c.Id = data.Id
	return nil
}

func (e *Notice) Delete(c *dto.NoticeDeleteReq) error {
	err := e.Orm.Model(&notice.Notice{}).Where("id=?", c.Id).Delete(&notice.Notice{}).Error
	if err != nil {
		e.Log.Errorf("删除公告失败:%s \r\n", err)
		return err
	}
	return nil
}

func (e *Notice) GetNotice(c *dto.NoticeGetReq) (notice.Notice, error) {
	var data notice.Notice
	err := e.Orm.Model(&notice.Notice{}).Where("bind_id=?", c.BindId).Order("update_time desc").Find(&data).Error
	if err != nil {
		e.Log.Errorf("获取公告失败:%s \r\n", err)
		return data, err
	}
	return data, nil
}
